package navigation.wzg.com.navigation;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import navigation.wzg.com.navigation.Write.Shareds;
import navigation.wzg.com.navigation.activities.Activity_menu_login;
import navigation.wzg.com.navigation.fragment.NewsActivity;

public class Welcome extends AppCompatActivity {

    public static Activity ActivityA;
    @BindView(R.id.welcome_countdown)
    TextView welcomeCountdown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        ActivityA = this;
        countdown();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                init();
            }
        }, 2500);
    }

    public void countdown() {
        final int[] a = {2};
        final Timer time = new Timer();
        TimerTask ttt = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        a[0]--;
                        welcomeCountdown.setText("倒计时" + a[0] + "s");
                        if (a[0] == 0) {
                            time.cancel();
                        }
                    }
                });
            }
        };
        time.schedule(ttt, 1000, 1000);
    }

    private void init() {
        if (!Shareds.Firsttrue("isFirstIn", Welcome.this)) {
            startActivity(new Intent(Welcome.this, Activity_menu_login.class));
        } else {
            startActivity(new Intent(Welcome.this, NewsActivity.class));
            sky();
            Shareds.First("isFirstIn", false, Welcome.this);
        }

    }

    public void sky() {
        finish();
        overridePendingTransition(R.anim.out, R.anim.up);
    }

    public void onBackPressed() {

    }
}
